/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vce.ant;

import com.vce.*;
import com.vce.election.domain.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Ggutierrez
 */
public class CreateBaselineDataTask extends Task {

    private Session session;
    private SimpleDateFormat dateformat = new SimpleDateFormat("MM-dd-yyyy");
    private SimpleDateFormat timestampformat = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");

    public CreateBaselineDataTask() {
    }

    public static void main(String[] args) {
        Task task = new CreateBaselineDataTask();
        task.execute();
    }

    public void execute() throws BuildException {
        Configuration cfg = new Configuration();
        cfg.configure();
        SessionFactory sf = cfg.buildSessionFactory();
        session = sf.openSession();
        Transaction tx = session.beginTransaction();
        try {
            createYear();
            createElectionType();
            createUser();
            createParty();
            createPosition();
            createCandidate();
//            createState();
//            createTown();
//            createBallotCentre();

            tx.commit();
        } catch (Exception e) {
            log(e, 0);
            e.printStackTrace();
            tx.rollback();
            throw new BuildException(e);
        } finally {
            session.close();
            sf.close();
        }
    }

    public void createYear() {
        session.persist(new Year(2006, "Year 2006", false));
        session.persist(new Year(2008, "Year 2008", true));
    }

    public void createElectionType() {
        Year year2006 = (Year) session.get(Year.class, 1L);
        Year year2008 = (Year) session.get(Year.class, 2L);

        session.persist(new ElectionType(year2006, "Presidente", false));
        session.persist(new ElectionType(year2006, "Diputados", false));
        session.persist(new ElectionType(year2008, "Alcalde / Vice-Alcalde", true));
        session.persist(new ElectionType(year2008, "Concejales",false));
    }
    
    public void createUser(){
        User user1 = new User("Giovanella","Gutierrez","ggutierrez","abc",'A');
        User user2 = new User("Guilber","Castillo","gcastillo","abc",'A');
        session.persist(user1);
        session.persist(user2);
        // Default Users
        session.persist(new User("Kitty","Monterrey","kmonterrey","kmonterrey!",'A'));
        session.persist(new User("Eduardo","Montealegre","emontealegre","emontealegre!",'A'));
        session.persist(new User("Gerardo","Rivas","grivas","grivas!",'A'));
        session.persist(new User("Operador 1","Managua","mga_ope01","mga_ope01",'O'));
        session.persist(new User("Operador 2","Managua","mga_ope02","mga_ope02",'O'));
        session.persist(new User("Supervisor 1","Managua","mga_sup01","mga_sup01",'S'));
    }
    
    public void createParty(){
        ElectionType electionType = (ElectionType) session.get(ElectionType.class, 1L);
               
        session.persist(new Party(electionType,"PLC","PARTIDO LIBERAL CONSTITUCIONALISTA",1));
        session.persist(new Party(electionType,"FSLN","FRENTE SANDINISTA DE LIBERACI�N NACIONAL",2));
        session.persist(new Party(electionType,"CCN","CAMINO CRISTIANO NICARAG�ENSE",3));
        session.persist(new Party(electionType,"PAMUC","PARTIDO MOVIMIENTO DE UNIDAD COSTE�A",6));
        session.persist(new Party(electionType,"YATAMA","YAPTI TASBA MASRAKA NANIH ASLA TAKANKA",8));
        session.persist(new Party(electionType,"ALN","ALIANZA LIBERAL NICARAG�ENSE",9));
        session.persist(new Party(electionType,"APRE","ALIANZA POR LA REP�BLICA",10));
        session.persist(new Party(electionType,"MRS","MOVIMIENTO RENOVADOR SANDINISTA",14));
        
        electionType = (ElectionType) session.get(ElectionType.class, 3L);
        session.persist(new Party(electionType,"PLC","PARTIDO LIBERAL CONSTITUCIONALISTA",1));
        session.persist(new Party(electionType,"FSLN","FRENTE SANDINISTA DE LIBERACI�N NACIONAL",2));
        session.persist(new Party(electionType,"ALN","ALIANZA LIBERAL NICARAG�ENSE",9));
        session.persist(new Party(electionType,"PRN","PARTIDO RESISTENCIA NACIONAL",14));
        session.persist(new Party(electionType,"AC","ALIANZA CRISTIANA",16));
    }
    
    public void createPosition(){
        session.persist(new Position("Presidente",1));
        session.persist(new Position("Vice-Presidente",2));
        session.persist(new Position("Diputado",3));
        session.persist(new Position("Alcalde",1));
        session.persist(new Position("Vice-Alcalde",2));
        session.persist(new Position("Concejal",3));
        session.persist(new Position("Esca�o",1));
    }
    
    public void createCandidate(){
        Year year2006 = (Year) session.get(Year.class, 1L);
        Party party = (Party) session.get(Party.class, 11L);
        Position position = (Position) session.get(Position.class, 1L);
        session.persist(new Candidate(year2006, party, "Eduardo", "Montealegre", position));
        
        Year year2008 = (Year) session.get(Year.class, 2L);
        party = (Party) session.get(Party.class, 9L);
        position = (Position) session.get(Position.class, 3L);
        session.persist(new Candidate(year2008, party, "Eduardo", "Montealegre", position));
    }
    
    public void createState(){
        try {
            User user = (User) session.get(User.class, 1L);
            Audit audit = new Audit(user, dateformat.parse("05-07-2008"), user, dateformat.parse("05-07-2008"));
            
            session.persist(new State(0,"DESCONOCIDO", false, 0, audit));
            session.persist(new State(1,"NUEVA SEGOVIA", false, 0, audit));
            session.persist(new State(2,"MADRIZ", false, 0, audit));
            session.persist(new State(3,"ESTELI", false, 0, audit));
            session.persist(new State(4,"CHINANDEGA", false, 0, audit));
            session.persist(new State(5,"LEON", false, 0, audit));
            session.persist(new State(6,"MANAGUA", true, 0, audit));
            session.persist(new State(7,"MASAYA", false, 0, audit));
            session.persist(new State(8,"CARAZO", false, 0, audit));
            session.persist(new State(9,"GRANADA", false, 0, audit));
            session.persist(new State(10,"RIVAS", false, 0, audit));
            session.persist(new State(11,"CHONTALES", false, 0, audit));
            session.persist(new State(12,"BOACO", false, 0, audit));
            session.persist(new State(13,"MATAGALPA", false, 0, audit));
            session.persist(new State(14,"JINOTEGA", false, 0, audit));
            session.persist(new State(15,"RAAN", false, 0, audit));
            session.persist(new State(16,"RAAS", false, 0, audit));
            session.persist(new State(17,"RIO SAN JUAN", false, 0, audit));
        } catch (ParseException ex) {
            Logger.getLogger(CreateBaselineDataTask.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void createTown(){
        try {
            User user = (User) session.get(User.class, 1L);
            Audit audit = new Audit(user, dateformat.parse("05-07-2008"), user, dateformat.parse("05-07-2008"));
            
            State state = (State)session.get(State.class, 6L);
            session.persist(new Town(state, 1, "SAN FRANCISCO LIBRE", 5L, true, audit));
            session.persist(new Town(state, 2, "TIPITAPA", 3L, true, audit));
            session.persist(new Town(state, 3, "MATEARE", 6L, true, audit));
            session.persist(new Town(state, 4, "VILLA EL CARMEN", 4L, true, audit));
            session.persist(new Town(state, 5, "MANAGUA", 1L, true, audit));
            session.persist(new Town(state, 6, "TICUANTEPE", 7L, true, audit));
            session.persist(new Town(state, 7, "SAN RAFAEL DEL SUR", 2L, true, audit));
            session.persist(new Town(state, 8, "CIUDAD SANDINO", 8L, true, audit));
            session.persist(new Town(state, 9, "EL CRUCERO", 9L, true, audit));
            session.persist(new Town(state, 5, "MANAGUA - DISTRITO II", 630L, true, audit));
            session.persist(new Town(state, 5, "MANAGUA - DISTRITO III", 631L, true, audit));
            session.persist(new Town(state, 5, "MANAGUA - DISTRITO IV", 632L, true, audit));
            session.persist(new Town(state, 5, "MANAGUA - DISTRITO V", 633L, true, audit));
            session.persist(new Town(state, 5, "MANAGUA - DISTRITO VI", 634L, true, audit));
        } catch (ParseException ex) {
            Logger.getLogger(CreateBaselineDataTask.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void createBallotCentre(){
        try {
            Year year = (Year) session.get(Year.class, 2L);
            User user = (User) session.get(User.class, 1L);
            Audit audit = new Audit(user, dateformat.parse("05-07-2008"), user, dateformat.parse("05-07-2008"));

            Town town = (Town) session.get(Town.class, 11L);
            session.persist(new BallotCentre(year, town, 855, "COLEGIO MADRE DEL DIVINO PASTOR", "", town.getRegistral(), audit));
            session.persist(new BallotCentre(year, town, 230, "UNI", "", town.getRegistral(), audit));
        } catch (ParseException ex) {
            Logger.getLogger(CreateBaselineDataTask.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void createJrv(){
        try {
            Year year = (Year) session.get(Year.class, 2L);
            User user = (User) session.get(User.class, 1L);
            Audit audit = new Audit(user, dateformat.parse("05-07-2008"), user, dateformat.parse("05-07-2008"));

            Town town = (Town) session.get(Town.class, 11L);
            session.persist(new BallotCentre(year, town, 855, "COLEGIO MADRE DEL DIVINO PASTOR", "", town.getRegistral(), audit));
            session.persist(new BallotCentre(year, town, 230, "UNI", "", town.getRegistral(), audit));
        } catch (ParseException ex) {
            Logger.getLogger(CreateBaselineDataTask.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
